from django.http import JsonResponse
from django.shortcuts import render, HttpResponse, redirect
from web.forms.project import ProjectModelForm
from web import models
""" 项目列表 """
def project_list(request):
    if request.method == 'GET':
        """
        GET请求查看项目列表
        1. 从数据库中获取两部分数据
	        我创建的所有项目：已星标、未星标
	        我参与的所有项目：已星标、未星标
        2. 提取已星标项目
	        列表 = 循环 [我创建的所有项目] + [我参与的所有项目] 把已星标的数据提取
	
        得到三个列表：星标、我创建、我参与
        """
        project_dict = {'star':[], 'my':[], 'join':[]}
        # 我创建的项目
        my_created_project_list = models.Project.objects.filter(
            creator=request.tracer.user
        )
        for row in my_created_project_list:
            if row.star:
                project_dict['star'].append({'value': row, 'type': 'my'})
            else:
                project_dict['my'].append(row)
        # 我参与的项目
        my_joined_project_list = models.ProjectUser.objects.filter(
            user=request.tracer.user
        )
        for item in my_joined_project_list:
            if item.star:
                project_dict['star'].append({'value': item.project, 'type': 'join'})
            else:
                project_dict['join'].append(item.project)

        form = ProjectModelForm(request)
        return render(request, 'web/project_list.html', {'form': form, 'project_dict': project_dict})
    # print(request.POST)
    form = ProjectModelForm(request, data=request.POST)
    if form.is_valid():
        # 验证通过：项目名，颜色，描述 + creator创建者
        form.instance.creator = request.tracer.user
        form.save()
        return JsonResponse({'status': True})
    return JsonResponse({'status': False, 'error': form.errors})


""" 添加项目星标 """
def project_star(request, project_type, project_id):
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=True)
        return redirect('project_list')

    if project_type == 'join':
        models.ProjectUser.objects.filter(project_id=project_id, user=request.tracer.user).update(star=True)
        return redirect('project_list')

    return HttpResponse("请求错误")

""" 取消项目星标 """
def project_unstar(request, project_type, project_id):
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=False)
        return redirect('project_list')

    if project_type == 'join':
        models.ProjectUser.objects.filter(project_id=project_id, user=request.tracer.user).update(star=False)
        return redirect('project_list')

    return HttpResponse("请求错误")